﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace TeacherHelp.FormList.Teacher
{
    public partial class ExercisesEditForm : Form
    {
        private readonly TeacherMainForm _parentForm;
        private readonly string _exercisesId;
        public ExercisesEditForm()
        {
            InitializeComponent();
        }

        public ExercisesEditForm(TeacherMainForm parent, string exercisesId) : this()
        {
            _parentForm = parent;
            _exercisesId = exercisesId;
        }

        private void ExercisesEditForm_Load(object sender, EventArgs e)
        {
            //题目难度
            List<dynamic> difficultylist = new List<dynamic>{
                new { Id = 1, Value = "简单" },
                new { Id = 2, Value = "中等" },
                new { Id = 3, Value = "困难" }
            };
            this.ExercisesDifficultyComBox.DataSource = difficultylist;
            this.ExercisesDifficultyComBox.DisplayMember = "Value";
            this.ExercisesDifficultyComBox.ValueMember = "Id";


            //科目类型
            BLL.SubjectsManager subjectsManager = new BLL.SubjectsManager();
            this.SubjectsTypeComBox.DataSource = subjectsManager.GetSubejctAll();
            this.SubjectsTypeComBox.DisplayMember = nameof(Model.Subjects.Name);
            this.SubjectsTypeComBox.ValueMember = nameof(Model.Subjects.Id);

            //初始化值
            BLL.ExercisesManager exercisesManager = new BLL.ExercisesManager();
            ViewModel.Exercises exercises = exercisesManager.GetExerciseModel(_exercisesId);
            ExercisesAnswerTxt.Text = exercises.Answer;
            OptionATxt.Text = exercises.Aoption;
            OptionBTxt.Text = exercises.Boption;
            OptionCTxt.Text = exercises.Coption;
            OptionDTxt.Text = exercises.Doption;
            ExercisesTxt.Text = exercises.Content;
            ExercisesDifficultyComBox.Text = exercises.Difficulty;
            SubjectsTypeComBox.Text = exercises.SubjectName;

        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            Model.Exercises exercises = new Model.Exercises();
            exercises.Id = _exercisesId.ToInt();
            exercises.Content = this.ExercisesTxt.Text;
            exercises.SubjectsId = this.SubjectsTypeComBox.SelectedValue.ToInt();
            exercises.Answer = this.ExercisesAnswerTxt.Text.Trim();
            exercises.Difficulty = this.ExercisesDifficultyComBox.SelectedValue.ToInt();
            exercises.Aoption = this.OptionATxt.Text.Trim();
            exercises.Boption = this.OptionBTxt.Text.Trim();
            exercises.Coption = this.OptionCTxt.Text.Trim();
            exercises.Doption = this.OptionDTxt.Text.Trim();

            if (string.IsNullOrWhiteSpace(exercises.Content))
            {
                MessageBox.Show("题目内容禁止为空！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrWhiteSpace(exercises.Answer))
            {
                MessageBox.Show("答案禁止为空！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrWhiteSpace(exercises.Aoption))
            {
                MessageBox.Show("选项A不允许为空！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrWhiteSpace(exercises.Boption))
            {
                MessageBox.Show("选项B不允许为空！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //答案数据处理
            Regex regex = new Regex("[a-d|A-D]{1}");
            var answerlist = regex.Matches(exercises.Answer);
            List<string> newAnswerlist = new List<string>();
            foreach (var item in answerlist)
            {
                newAnswerlist.Add(item.ToString().ToUpper());
            }
            newAnswerlist = newAnswerlist.Distinct().ToList();
            exercises.Answer = string.Join(",", newAnswerlist);

            if (string.IsNullOrWhiteSpace(exercises.Coption) && string.IsNullOrWhiteSpace(exercises.Doption))
            {
                if (newAnswerlist.Count > 1)
                {
                    MessageBox.Show("判断题只能为单选！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }

            if (newAnswerlist.Contains("C") && string.IsNullOrWhiteSpace(exercises.Coption))
            {
                MessageBox.Show("选项C不允许为空！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (newAnswerlist.Contains("D") && string.IsNullOrWhiteSpace(exercises.Doption))
            {
                MessageBox.Show("选项D不允许为空！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            BLL.ExercisesManager exercisesManager = new BLL.ExercisesManager();
            var result = exercisesManager.ExercisesEdit(exercises);
            MessageBox.Show(result ?? "保存成功！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Information);
            _parentForm.ChildFormInt();
        }

        private void ExercisesAnswerTxt_TextChanged(object sender, EventArgs e)
        {
            Regex regex = new Regex("[^A-D|,|a-d]");
            ExercisesAnswerTxt.Text = regex.Replace(ExercisesAnswerTxt.Text, "");
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            _parentForm.ChildFormInt();
        }
    }
}
